package net.ojbk.timeAxis.service;

import org.apache.ibatis.session.SqlSession;

import net.ojbk.timeAxis.po.User;
import net.ojbk.timeAxis.tool.DBTool;

public class LoginAndRegisterServlice {
	
	public int login(String userId, String userPW) {
		SqlSession sqlSession = DBTool.getSession();
		
		try {
			User u = sqlSession.selectOne("userMap.selectOneById", userId);
			if (u == null) {
				return 2;
			}
			String password = u.getUserPW();
			if (password==null || password.equals("")) { // 密码不存在 也就是无账户
				return 2;
			} else if (!password.equals(userPW)) { // 密码不相符合
				return 3;
			} else { // 帐户密码正确
				return 1;
			}
		} finally {
			
			sqlSession.close();
		}
	}
	
	// 1成功 5失败
	public int register(String userId, String userPW) {
		SqlSession sqlSession = DBTool.getSession();
		
		try {
			User u = new User();
			u.setUserId(userId);
			u.setUserPW(userPW);
			
			sqlSession.insert("userMap.insertOne", u);
			sqlSession.commit();
			
		} catch(Exception e) {
			return 5;
		} finally {
			sqlSession.close();
		}
		return 1;
	}
	
}
